package com.canaan.business.service.bill.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.canaan.business.common.enums.storehouse.StorehouseStockStatusEnum;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.domain.vo.OutStockQueryVo;
import com.canaan.business.service.agv.IAgvTaskDispatchService;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.TrayDetailMapper;
import com.canaan.business.domain.bill.TrayDetail;
import com.canaan.business.service.bill.ITrayDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 托盘明细Service业务层处理
 *
 * @author canaan
 * @date 2025-03-28
 */
@Service
public class TrayDetailServiceImpl implements ITrayDetailService {
    @Resource
    private TrayDetailMapper trayDetailMapper;
    @Resource
    private IAgvTaskDispatchService agvTaskDispatchService;

    /**
     * 查询托盘明细
     *
     * @param id 托盘明细主键
     * @return 托盘明细
     */
    @Override
    public TrayDetail selectTrayDetailById(Long id) {
        return trayDetailMapper.selectTrayDetailById(id);
    }

    /**
     * 查询托盘明细列表
     *
     * @param trayDetail 托盘明细
     * @return 托盘明细
     */
    @Override
    public List<TrayDetail> selectTrayDetailList(TrayDetail trayDetail) {
        if (BeanUtil.isEmpty(trayDetail)||StrUtil.equalsIgnoreCase("", trayDetail.getTrayNo())) {
            return null;
        }
        return trayDetailMapper.selectTrayDetailList(trayDetail);
    }

    @Override
    public List<TrayDetail> selectTrayDetailsByOutStockQueryVo(OutStockQueryVo outStockQueryVo){
        return trayDetailMapper.selectTrayDetailsByOutStockQueryVo(outStockQueryVo);
    }

    /**
     * 新增托盘明细
     *
     * @param trayDetail 托盘明细
     * @return 结果
     */
    @Override
    public int insertTrayDetail(TrayDetail trayDetail) {
        trayDetail.setCreateBy(SecurityUtils.getUsername());
        trayDetail.setCreateTime(DateUtils.getNowDate());
        return trayDetailMapper.insertTrayDetail(trayDetail);
    }

    /**
     * 批量新增托盘明细
     *
     * @param trayDetailList 托盘明细列表
     * @return 结果
     */
    @Override
    public int batchInsertTrayDetail(List<TrayDetail> trayDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayDetail trayDetail : trayDetailList){
            trayDetail.setCreateBy(username);
            trayDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<trayDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > trayDetailList.size()) {
                endIndex = trayDetailList.size();
            }
            insertNum = insertNum + trayDetailMapper.batchInsertTrayDetail(trayDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改托盘明细
     *
     * @param trayDetail 托盘明细
     * @return 结果
     */
    @Override
    public int updateTrayDetail(TrayDetail trayDetail) {
        trayDetail.setUpdateBy(SecurityUtils.getUsername());
        trayDetail.setUpdateTime(DateUtils.getNowDate());
        return trayDetailMapper.updateTrayDetail(trayDetail);
    }

    /**
     * 批量修改托盘明细
     *
     * @param trayDetailList 托盘明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateTrayDetail(List<TrayDetail> trayDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayDetail trayDetail : trayDetailList){
            trayDetail.setUpdateBy(username);
            trayDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<trayDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > trayDetailList.size()) {
                endIndex = trayDetailList.size();
            }
            updateNum = updateNum + trayDetailMapper.batchUpdateTrayDetail(trayDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除托盘明细
     *
     * @param ids 需要删除的托盘明细主键集合
     * @return 结果
     */
    @Override
    public int deleteTrayDetailByIds(Long[] ids) {
        return trayDetailMapper.deleteTrayDetailByIds(ids);
    }

    /**
     * 删除托盘明细信息
     *
     * @param id 托盘明细主键
     * @return 结果
     */
    @Override
    public int deleteTrayDetailById(Long id) {
        return trayDetailMapper.deleteTrayDetailById(id);
    }

    /**
     * 根据托盘号删除明细数据
     * @param trayNo 托盘号
     * @return
     */
    public int deleteTrayDetailByTrayNo(String trayNo){
        if (StringUtils.isEmpty(trayNo)) {
            return 0;
        }
        TrayDetail trayDetail = new TrayDetail();
        trayDetail.setTrayNo(trayNo);
        List<TrayDetail> trayDetails = trayDetailMapper.selectTrayDetails(trayDetail);
        if (CollUtil.isEmpty(trayDetails)) {
            return 0;
        }
        if (agvTaskDispatchService.queryRunningTaskByTrayNo(trayNo) > 0) {
            return 0;
        }
        return trayDetailMapper.deleteTrayDetailByTrayNo(trayNo);
    }

    @Override
    public AjaxResult selectTrayDetails(TrayDetail trayDetail) {
        List<TrayDetail> list = trayDetailMapper.selectTrayDetails(trayDetail);
        if (CollUtil.isNotEmpty(list)) {
            return AjaxResult.success("查询成功", list);
        }
        return AjaxResult.error("没有查询到数据");
    }

    @Override
    public AjaxResult unbindProductNo(TrayDetail vo) {
        if (StringUtils.isEmpty(vo.getTrayNo())) {
            return AjaxResult.error("托盘编号不能为空");
        }
        if (StringUtils.isEmpty(vo.getProductNo())) {
            return AjaxResult.error("物料号不能为空");
        }
        if (StringUtils.isEmpty(vo.getBatchNo())) {
            return AjaxResult.error("批次号不能为空");
        }
        List<TrayDetail> trayDetails = trayDetailMapper.selectTrayDetails(vo);
        if (CollUtil.isEmpty(trayDetails)) {
            return AjaxResult.error("托盘没有绑定料架");
        }
        if (agvTaskDispatchService.queryRunningTaskByTrayNo(vo.getTrayNo()) > 0) {
            return AjaxResult.error("存在AGV搬运任务，不可解绑");
        }
        trayDetailMapper.deleteTrayDetailByTrayNoProdNoBatch(vo.getTrayNo(), vo.getProductNo(), vo.getBatchNo());
        return AjaxResult.success("解绑成功");
    }
}
